<?php
/**
 * This file houses the ReflectionTestClass class.
 *
 * Copyright (c) 2010, Kalkomey Enterprises, Inc.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice, this 
 *   list of conditions and the following disclaimer in the documentation and/or other
 *   materials provided with the distribution.
 * - Neither the name of the Kalkomey Enterprises, Inc. nor the names of its 
 *   contributors may be used to endorse or promote products derived from this 
 *   software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @copyright Copyright (c) 2010, Kalkomey Enterprises, Inc.
 * @link http://code.google.com/p/swerv
 * @package Framework
 * @subpackage Tests
 */

/**
 * The ReflectionTestClass class includes several methods used to manipulate and display strings.  It is only used as an example class for testing the ClassReflector class.
 *
 * @package Framework
 * @subpackage Tests
 */
class ReflectionTestClass {

	/**
	 * Simple test property.
	 *
	 * @var string
	 */
	public $testProperty;

	/**
	 * Coverts a string from this_notation to ThisNotation (PascalCase)
	 *
	 * @param string $noPascal the string in this_notation
	 *
	 * @return string
	 */
	public function toPascalCase($noPascal) {
		$noPascal = strtolower($noPascal);
		$noPascal = str_replace('_', ' ', $noPascal);
		$noPascal = ucwords($noPascal);
		$pascal = str_replace(' ', '', $noPascal);
		return $pascal;
	}

	/**
	 * Coverts a string from this_notation to thisNotation (camelCase)
	 *
	 * @param string $noCamel the string in this_notation
	 *
	 * @return string
	 */
	public function toCamelCase($noCamel) {
		$noCamel = strtolower($noCamel);
		$parts = explode('_', $noCamel);
		$x = 1;
		$camel = '';
		foreach ($parts as $part) {
			if ($x > 1) {
				$part = ucwords($part);
			}
			$camel .= $part;
			$x++;
		}
		return $camel;
	}
	
	/**
	 * Converts a PascalCase string into individual words (Pascal Case).
	 *
	 * @uses StringUtility::pascalToArray()
	 *
	 * @param string $pascal the string in ThisNotation
	 *
	 * @return string
	 */
	public function pascalCaseToWords($pascal) {
		$array = self::pascalToArray($pascal);
		$words = implode(' ', $array);
		return $words;
	}

	/**
	 * Converts a PascalCase string into individual words stored in a numerical indexed array.
	 *
	 * @param string $pascal the string in ThisNotation
	 *
	 * @exposePublic
	 *
	 * @return array
	 */
	public function pascalToArray($pascal) {
		// capitalize the first character so the splitting works correctly
		$firstChar = substr($pascal, 0, 1);
		$firstChar = strtoupper($firstChar);
		$pascal = $firstChar . substr($pascal, 1, strlen($pascal));
		
		// split up the string into an array according to the uppercase characters
		$array = preg_split('/([A-Z][^A-Z]*)/', $pascal, (-1), PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
		return $array;
	}
	
	/**
	 * Converts a string from PascalCase to this_notation.
	 *
	 * @uses StringUtility::pascalToArray()
	 *
	 * @param string $pascal a string in PascalCase
	 *
	 * @return string
	 */
	public function toLowerCase($pascal) {
		// if there are already underscores, we're pretty much done
		if (stripos($pascal, '_') !== false) {
			return strtolower($pascal);
		}
		$array = self::pascalToArray($pascal);
		$array = array_map('strtolower', $array);
		$lower = implode('_', $array);
		return $lower;
	}

	/**
	 * Used primarily by the Cron related error/exception handlers, this function writes a string to the terminal, wrapping it at 80 characters.
	 *
	 * @param string $txt the text to write to the terminal
	 * @param integer $ind the number of spaces to indent each line (after wrapping)
	 *
	 * @return void
	 */
	public function writeLine($txt, $ind = 0) {
		$obj = (Object) array();
		$obj->text = $txt;
		$obj->indent = $ind;
		$max_line_len = 79;
		$wrap_point = $max_line_len - $obj->indent - 1;
		$indent = '';
		$all_lines = array();
		for ($i = 0; $i < $obj->indent; $i++) {
			$indent .= " ";
		}
		$lines_str = wordwrap($obj->text, $wrap_point, "---");
		$lines = explode("---", $lines_str);
		$body = '';
		foreach ($lines as $line) {
			$all_lines[] = $indent . $line;
		}
		foreach ($all_lines as $line) {
			$body .= $line;
			for ($i = 0; $i < $max_line_len - strlen($line); $i++) {
				$body .= " ";
			}
			$body .= "\n";
		}
		echo $body;
		return;
	}

	/**
	 * Adds single quotes and escapes single quotes and backslashes to the given string
	 *
	 * @param string $arg the string we need to quote
	 *
	 * @return string
	 */
	public function addSingleQuotes($arg) { 
		/* single quote and escape single quotes and backslashes */ 
		return "'" . addcslashes($arg, "'\\") . "'"; 
	}
	
	/**
	 * Formats an XML string for easier viewing.
	 *
	 * @param string $xml the XML to reformat
	 *
	 * @return string
	 */
	public function formatXmlString($xml) {  
		$xml = preg_replace('/(>)(<)(\/*)/', "$1\n$2$3", $xml);
		$token = strtok($xml, "\n");
		$result = '';
		$pad = 0;
		$matches = array();
		while ($token !== false) {
			if (preg_match('/.+<\/\w[^>]*>$/', $token, $matches)) { 
				$indent=0;
			} elseif (preg_match('/^<\/\w/', $token, $matches)) {
				$pad--;
			} elseif (preg_match('/^<\w[^>]*[^\/]>.*$/', $token, $matches)) {
				$indent=1;
			} else {
				$indent = 0; 
			}
			$line = str_pad($token, strlen($token)+$pad, "\t", STR_PAD_LEFT);
			$result .= $line . "\n";
			$token = strtok("\n");
			$pad += $indent;
		}
		return $result;
	}			
}
